package com.example.demo.controller;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/login")
public class LoginController {
    @RequestMapping("/check")
    public boolean login(String username, String password, HttpSession session) {
        // 校验账号密码是否为空
//        if (username == null || "".equals(username) || password == null || "".equals(password)) {
//            return false;
//        }
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password) ) {
            return false;
        }
        if ("zhangsan".equals(username) && "123456".equals(password)) { // 这样写是为了防止空指针
            session.setAttribute("username", username);
            session.setAttribute("password", password);
            return true;
        }
        return false;
    }

    @RequestMapping("/index")
    public String get(HttpSession session) {
        String username = (String) session.getAttribute("username");
        return username;
    }
}
